//
//  MuchViewController.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/14.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "MuchViewController.h"

@interface MuchViewController ()

@end

@implementation MuchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isLoad = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.sectionArray = [NSMutableArray arrayWithCapacity:0];
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    self.condition = @"hot";
    self.title = @"最热";
    
    self.navigationController.navigationBar.tintColor = kColor;
    [self.navigationController.navigationItem.leftBarButtonItem setTitle:@"主播"];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:20],
       NSForegroundColorAttributeName:kColor}];
    [self creatViews];
    
    
}

- (void)getColumnData:(NSString *)url {
    [SANetWorkingTask request:url parmater:nil block:^(id result) {
        self.columnArray = [NSMutableArray arrayWithCapacity:0];
        [self.columnArray addObjectsFromArray:result[@"data"][@"ca"]];
    }];
}

- (void)creatViews {
    //    if ([self.condition isEqualToString:@"new"] && [self.name isEqualToString:@"all"]) {
    //
    //    } else {
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"最热", @"最新", nil]];
    self.segmentedControl.backgroundColor = [UIColor whiteColor];
    self.segmentedControl.tintColor = [UIColor colorWithRed:0.85 green:0.37 blue:0.29 alpha:1];
    self.segmentedControl.backgroundColor = [UIColor whiteColor];
    //        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, [UIFont systemFontOfSize:16 weight:1], UITextAttributeTextShadowColor, nil];
    //        [self.segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];
    self.segmentedControl.frame = CGRectMake(0, 64, self.view.width, 40);
    [self.segmentedControl addTarget:self action:@selector(segmengtAction:) forControlEvents:UIControlEventValueChanged];
    self.segmentedControl.selectedSegmentIndex = 0;
    [self.view addSubview:self.segmentedControl];
    //    }
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 104, self.view.frame.size.width, self.view.frame.size.height - 64 - self.segmentedControl.height - 40) style:UITableViewStyleGrouped];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.delegate = self;
    //    self.tableView.contentInset = UIEdgeInsetsMake(-40, 0, 0, 0);
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    self.tableView.backgroundColor = [UIColor colorWithRed:0.17 green:0.19 blue:0.25 alpha:0.6];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getDataByUrl:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_list?category_name=%@&condition=%@&device=android&page=%@&per_page=20",self.name,self.condition,@"1"]];
        self.isLoad = NO;
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSString *nextPage = [NSString stringWithFormat:@"%d", self.currentPage.intValue + 1];
        [self getDataByUrl:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_list?category_name=%@&condition=%@&device=android&page=%@&per_page=20",self.name,self.condition,nextPage]];
        self.isLoad = YES;
    }];
    
//    NSLog(@"%f", self.tableView.origin.y);
    
    [self.tableView registerClass:[ChangeAlbumTableViewCell class] forCellReuseIdentifier:@"AlbumTableViewCell"];
    
    [self.view addSubview:self.tableView];
    [self.tableView.header beginRefreshing];
    
    [self.view addSubview:self.tableView];
}

- (void)getDataByUrl:(NSString *)url {
    
    //    NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:0];
    //    NSMutableArray *section = [NSMutableArray arrayWithCapacity:0];
    
    [SANetWorkingTask request:url parmater:nil block:^(id result) {
        if (!self.isLoad) {
            [self.sectionArray removeAllObjects];
            [self.dataSource removeAllObjects];
        }
        
        self.maxPage = result[@"maxPageId"];
        self.currentPage = result[@"pageId"];
        for (NSDictionary *dic in result[@"list"]) {
            AnchorModel *model = [[AnchorModel alloc] initWithDictionary:dic];
            [self.dataSource addObject:model];
        }
//                self.dataSource = [NSMutableArray arrayWithArray:dataArray];
        
        if (self.isLoad) {
            
        } else {
            [self.sectionArray addObject:@{@"sectionArr":self.dataSource}];
        }
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    }];
    
}

- (void)segmengtAction:(UISegmentedControl *)segmentedController {
    self.isLoad = !self.isLoad;
    if (self.segmentedControl.selectedSegmentIndex == 0) {
        self.condition = @"hot";
        self.title = @"最热";
    } else {
        self.condition = @"new";
        self.title = @"最新";
    }
    
    [self getDataByUrl:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_list?category_name=%@&condition=%@&device=android&page=%@&per_page=20",self.name,self.condition,@"1"]];
    [self.tableView.header beginRefreshing];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSDictionary *dic = self.sectionArray[section];
    NSArray *array = [dic valueForKey:@"sectionArr"];
    
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChangeAlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumTableViewCell"];
    cell.model = self.dataSource[indexPath.row];
    return cell;
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
